<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/9 0009
 * Time: 下午 2:53
 */

namespace DataAPI\Controller;

use DataAPI\Extend\ExtendApi;
use DataAPI\Model\ExcelModel;
use DataAPI\Model\UserModel;
use Think\Controller;

/**
 * Class LoginController
 * @package Home\Controller
 */
class UserController extends Controller {

    public function index(){
        $this->display('index1');

    }
    /**
     * 用户登录
     */
    public function login()
    {
        // 判断提交方式
        if (IS_POST) {
            // 实例化Login对象
            $login = D('data_users');

            // 自动验证 创建数据集
            if (!$data = $login->create()) {
                // 防止输出中文乱码
                header("Content-type: text/html; charset=utf-8");
                exit($login->getError());
            }

            // 组合查询条件
            $where = array();
            $where['username'] = $data['username'];
            $result = $login->where($where)->field('id,username,password')->find();

            // 验证用户名 对比 密码
            if ($result && $result['password'] == md5(md5($data['password']))) {
                // 存储session
                session('uid', $result['id']);          // 当前用户id
                session('username', $result['username']);   // 当前用户名
                echo "<script>alert('登录成功');parent.location.href='".U('Index/index')."';</script>";
//                $this->success('登录成功,正跳转至系统首页...', U('Index/index'));
            } else {
                echo "<script>alert('登录失败,用户名或密码不正确!');parent.location.href='".U('login')."';</script>";
            }
        } else {
            $this->display();
        }
    }

    /**
     * 商户认证(未完成)
     */
    public function merchantAuth(){

        $app_id = $_POST['app_id'];
        $app_key = $_POST['app_key'];

//        print_r(ExtendApi::init($app_id,$app_key)->customerChecked());die;
        if(ExtendApi::init($app_id,$app_key)->customerChecked()===true){
            $_SESSION['app_id'] = $app_id;
            $_SESSION['app_key'] = $app_key;
            echo "<script>alert('认证通过');parent.location.href='".U('register')."';</script>";
        }else{
            echo "<script>alert('认证失败');parent.location.href='".U('merchant_auth')."';</script>";
        }

    }

    /**
     * 忘记密码
     */
    public function modifyPassword(){
        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        $user = new UserModel();
        if($user->modifyPassword($data)){
            echo "<script>alert('修改成功');parent.location.href='".U('login')."';</script>";
        }else{
            echo "<script>alert('修改失败');parent.location.href='".U('modify_password')."';</script>";
        }

    }
    /**
     * 用户注册
     */
    public function register()
    {
        // 判断提交方式 做不同处理
        if (IS_POST) {
            // 实例化User对象
            $user = new UserModel();
            $data['username'] = $_POST['username'];
            $data['password'] = $_POST['password'];
            $data['app_id'] = $_SESSION['app_id'];
            $data['app_key'] = $_SESSION['app_key'];
            //插入数据库
            if ($id = $user->addUser($data)) {
                unset($_SESSION['app_id']);
                unset($_SESSION['app_key']);
                session('uid', $id);          // 当前用户id
                session('username', $data['username']);   // 当前用户名
                echo "<script>alert('设置成功');parent.location.href='".U('Index')."';</script>";
//                $this->success('设置成功', U('Index'), 2);
            } else {
                echo "<script>alert('设置失败');parent.location.href='".U('register')."';</script>";
                $this->error('设置失败');
            }
        } else {
            $this->display();
        }
    }

    /**
     * 用户注销
     */
    public function logout()
    {
        // 清楚所有session
        session(null);
        echo "<script>alert('退出成功');parent.location.href='".U('login')."';</script>";
    }


    /**
     * 验证码
     */
    public function verify()
    {
        // 实例化Verify对象
        $verify = new \Think\Verify();

        // 配置验证码参数
        $verify->fontSize = 14;     // 验证码字体大小
        $verify->length = 4;        // 验证码位数
        $verify->imageH = 34;       // 验证码高度
        $verify->useImgBg = true;   // 开启验证码背景
        $verify->useNoise = false;  // 关闭验证码干扰杂点
        $verify->entry();
    }
}